import pymysql
from pandas import DataFrame
import logging

class DataStorage:
    def __init__(self, db_config: dict):
        self.connection = pymysql.connect(**db_config)
        self.logger = logging.getLogger(__name__)

    def save_daily_data(self, data: DataFrame):
        with self.connection.cursor() as cursor:
            sql = """
            INSERT INTO daily_data (symbol, date, open, high, low, close, volume)
            VALUES (%s, %s, %s, %s, %s, %s, %s)
            ON DUPLICATE KEY UPDATE
            open=VALUES(open), high=VALUES(high), low=VALUES(low), close=VALUES(close), volume=VALUES(volume)
            """
            data_tuples = [tuple(row) for _, row in data.iterrows()]
            cursor.executemany(sql, data_tuples)
            self.connection.commit()
            self.logger.info(f"Saved {len(data)} rows to daily_data table")

    def __del__(self):
        self.connection.close()
